/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pigx.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.dto.RegimenStationDto;
import com.pig4cloud.pigx.admin.api.entity.OverallRegimenStation;
import com.pig4cloud.pigx.admin.api.entity.OverallRegimenStationInfo;
import com.pig4cloud.pigx.admin.api.vo.RegimenStationVo;
import com.pig4cloud.pigx.admin.converter.OverallRegimenStationConverter;
import com.pig4cloud.pigx.admin.mapper.OverallRegimenStationInfoMapper;
import com.pig4cloud.pigx.admin.mapper.OverallRegimenStationMapper;
import com.pig4cloud.pigx.admin.service.OverallRegimenStationService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 四全水情站点信息 服务实现类
 * </p>
 *
 * @author lengleng
 * @since 2018-01-20
 */
@Service
@AllArgsConstructor
public class OverallRegimenStationServiceImpl extends ServiceImpl<OverallRegimenStationMapper, OverallRegimenStation> implements OverallRegimenStationService {


    private final OverallRegimenStationMapper mapper;

    private final OverallRegimenStationConverter converter;

    private final OverallRegimenStationInfoMapper stationInfoMapper;


    /**
     * @param res
     * @return List<RegimenStationVo>
     * @author lfx
     * @date 2024/8/10 17:08
     * @Desc 查询水情站点信息列表
     **/
    @Override
    public List<RegimenStationVo> selectList(RegimenStationDto res) {
        List<OverallRegimenStation> regimenStations =
                mapper.selectList(Wrappers.lambdaQuery(OverallRegimenStation.class)
                        .eq(StringUtils.isNotEmpty(res.getStationType()), OverallRegimenStation::getStationType,
                                res.getStationType()));
        if (CollUtil.isEmpty(regimenStations)) {
            return Collections.emptyList();
        }
        List<RegimenStationVo> regimenStationVos = converter.doListToVoList(regimenStations);
        List<String> stationCodeList = regimenStationVos.stream()
                .map(RegimenStationVo::getStationCode).collect(Collectors.toList());
        // 根据编码查询最新数据
        List<OverallRegimenStationInfo> overallRegimenStationInfos = stationInfoMapper.selectLastDataList(stationCodeList);
        if(CollUtil.isNotEmpty(overallRegimenStationInfos)){
            regimenStationVos.forEach(regimenStationVo -> {
                Optional<OverallRegimenStationInfo> optional = overallRegimenStationInfos.stream()
                        .filter(stationInfo -> stationInfo.getStationCode().equals(regimenStationVo.getStationCode()))
                        .findAny();
                if(optional.isPresent()){
                    regimenStationVo.setWaterLevel(optional.get().getWaterLevel());
                    regimenStationVo.setTime(optional.get().getTime());
                }
           });
        }
        return regimenStationVos;
    }
}
